package his.controller;

import com.alibaba.fastjson.JSONObject;
import his.entity.Disease;
import his.service.DieaseService;
import his.service.impl.DieaseServiceImpl;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/disease/")
public class DieaseController {

    @Autowired
    private DieaseService dieaseService;

    @GetMapping("all")
    @ResponseBody
    public CommonResponse<List<Disease>> getall_diease()
    {

        return  dieaseService.getall_diease();
    }

    @PostMapping("add")
    @ResponseBody
    public CommonResponse<List<Disease>> add_diease(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String diseasecode=(String) jsonObject.get("diseasecode");
        String diseasename=(String) jsonObject.get("diseasename");
        String diseaseICD=(String) jsonObject.get("diseaseICD");
        int disecategoryID=(int) jsonObject.get("disecategoryID");
        return  dieaseService.add_diease(diseasecode,diseasename,diseaseICD,disecategoryID);
    }

    @PostMapping("modify/{sequence_no}")
    @ResponseBody
    public CommonResponse<List<Disease>> update_disecategory(@PathVariable int id, HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String diseasecode=(String) jsonObject.get("diseasecode");
        String diseasename=(String) jsonObject.get("diseasename");
        String diseaseICD=(String) jsonObject.get("diseaseICD");
        int disecategoryID=(int) jsonObject.get("disecategoryID");
        return  dieaseService.update_diease(id,diseasecode,diseasename,diseaseICD,disecategoryID);
    }

    @DeleteMapping("del/{sequence_no}")
    @ResponseBody
    public CommonResponse<List<Disease>> delete_disecategory(@PathVariable int id)
    {

        return  dieaseService.delete_diease(id);
    }

    @PostMapping("search/{keyword}")
    @ResponseBody
    public CommonResponse<List<Disease>> getall_disecategory_bycodename(@PathVariable("keyword") String keyword)
    {
        return  dieaseService.getall_diease_bycodename(keyword);
    }
}
